// pages/duchuntao/unit7/case7-1/case7-1.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
      idBack:"back",
      idClear:"clear",
      idPon:"+-",
      idPlus:"+",
      idMinus:"-",
      idMult:"*",
      idDiv:"/",
      id9:"9",
      id8:"8",
      id7:"7",
      id6:"6",
      id5:"5",
      id4:"4",
      id3:"3",
      id2:"2",
      id1:"1",
      id0:"0",
      idPoint:".",
      idIs:"=",
      screenData:"0",
      lastIsOperator:false,
      arr:[],
      logs:[]
    },
    history:function(){
      wx.navigateTo({
        url:"../../unit7/case7-1-3/list"
      });
    },
    clientButton:function(event){
      var id=event.target.id;
      var data=this.data.screenData;
      if(id==this.data.idBack)
      {
        if(data=="0")
        return;
        else{
          data=data.substring(0,data.length-1);
          if(data==""||data=="-"){
            data="0";
          }
        }
        this.data.arr.pop();
        this.setData({
          screenData:data
        });
  
      }else if(id==this.data.idClear)
      {
        this.setData({
          screenData:"0"
        });
        this.data.arr.length=0;
      }else if(id==this.data.idPon){
        if(data.substring(0,1)=="-"){
        data=data.substring(1,data.length);
        this.data.arr.shift();
      }else{
        data="-" + data;
        this.data.arr.unshift("-");
      }
      this.setData({
        screenData:data
      });
    }else if(id==this.data.idIs){
      var data=this.data.screenData;
      if(data=="0"){
        return;
      }
      //console.log(date);
      var lastWowd=data.substring(data.length-1,data.length);
      //console.log("lastWowd"+lastWowd);
      if(isNaN(lastWowd)){
        return;
      }
      var num="";
      var optArr=[];
      var arr=this.data.arr;
      console.log(arr);
      for(var i in arr){
        if(isNaN(arr[i])==false|| arr[i]==this.data.idPon|| arr[i]==this.data.idPoint){
          num +=arr[i];
        }else{
          optArr.push(Number(num));
          optArr.push(arr[i]);
          num="";
        }
      }
      optArr.push(Number(num));
      console.log(optArr);
      var result=Number(optArr[0])*1.0;
      for(var i=1;i<optArr.length;i++){
        if(isNaN(optArr[i])){
          if(optArr[i]==this.data.idPlus){
            result += Number(optArr[i+1]);
          }else if(optArr[i]==this.data.idMinus){
            result -= Number(optArr[i+1]);
          }else if(optArr[i]==this.data.idMult){
            result *= Number(optArr[i+1]);
          }else if(optArr[i]==this.data.idDiv){
            result /= Number(optArr[i+1]);
          }
        }
      }
      var log=data+"="+result;
      this.data.logs.push(log)
      wx.setStorageSync('callLogs',this.data.logs);
  
      this.data.arr.length=0;
      this.data.arr.push(result);
      this.setData({
        screenData:result
      });
     }else{
        if(data=="0"){
          if(id==this.data.idPlus ||
            id==this.data.idMinus ||
            id==this.data.idMult ||
            id==this.data.idDiv) {
              return;
            }
            this.setData({
              screenData:event.target.id
            });
            this.data.arr.push(id);
        }else{
          if(id==this.data.idPlus ||
            id==this.data.idMinus ||
            id==this.data.idMult ||
            id==this.data.idDiv){
              if(this.data.lastIsOperator==true){
                return;
              }
            }
            this.setData({
              screenData:data+event.target.id
            });
            this.data.arr.push(id);
            //console.log(this.data.arr);
            if(id==this.data.idPlus ||
              id==this.data.idMinus ||
              id==this.data.idMult ||
              id==this.data.idDiv){
                this.setData({
                  lastIsOperator:true
                });
              }else{
                this.setData({
                  lastIsOperator:false
                });
              }
        }
      }    
    },
    

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})